﻿//
// GNS_SysTenLayer.cpp
// GameHall
//
// Created by mr.l on 16/2/3.
//
//

#include "GNS_SysTenLayer.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "AudioMgr.h"
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

enum 
{
	BTN_BACK,
	SLIDER_MUSIC,
	SLIDER_EFFECT,
};
GNS_SysTenLayer::GNS_SysTenLayer()
{
 
}

GNS_SysTenLayer::~GNS_SysTenLayer()
{
    
}

GNS_SysTenLayer* GNS_SysTenLayer::createLayer()
{
    auto pGNS_SysTenLayer = GNS_SysTenLayer::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_SysTenLayer);
    return pGNS_SysTenLayer;
}

bool GNS_SysTenLayer::init()
{
    if ( !GNS_YJFLayer::init() )
    {
        return false;
    }
    return true;
}
void GNS_SysTenLayer::SetNodeName(std::string CsbName)
{
	m_RootNode = Loadcsb(CsbName);
	addChild(m_RootNode, 11);

	// 返回按钮
	auto pBackBtn = static_cast<Button*>(m_RootNode->getChildByName("Button_4"));
	pBackBtn->setTag(BTN_BACK);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_SysTenLayer::BtnCallBack, this));
	//音乐进度条
	auto pMusic = static_cast<Slider*>(m_RootNode->getChildByName("Slider_1"));
	pMusic->setTag(SLIDER_MUSIC);
	pMusic->addEventListenerSlider(this, sliderpercentchangedselector(GNS_SysTenLayer::sliderEvent));
	pMusic->setPercent(AudioMgr::getInstance()->getMusicVolume()*100);


	// 音效进度条
	auto pVoice = static_cast<Slider*>(m_RootNode->getChildByName("Slider_2"));
	pVoice->setTag(SLIDER_EFFECT);
	pVoice->addEventListenerSlider(this, sliderpercentchangedselector(GNS_SysTenLayer::sliderEvent));
	pVoice->setPercent(AudioMgr::getInstance()->getEffectVolume() * 100);
}

void GNS_SysTenLayer::sliderEvent(cocos2d::Ref *pSender, SliderEventType type)
{
	if (type == SLIDER_PERCENTCHANGED) {
		Slider* slider = dynamic_cast<Slider*>(pSender); 
		int slidertag = slider->getTag();
		float percent = slider->getPercent();
		switch (slidertag)
		{
		case SLIDER_MUSIC:
		{				 
			//获取当前音乐或者音效的百分比
			AudioMgr::getInstance()->setMusicVolume(percent/100);
			break;
		}
		case SLIDER_EFFECT:
		{
			AudioMgr::getInstance()->setEffectVolume(percent/100);
			break;
		}	
		default:
			break;
		}
		

	}
}

void GNS_SysTenLayer::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
		case BTN_BACK:
            {
				Show(false);
                break;
            }
            default:
                break;
        }
    }
}
